package com.chujian.Activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
import com.chujian.orderingsystem.R;

public class BookTimeActivity extends Activity{
	private DatePicker mDatePicker;
    private TimePicker mTimePicker;
    private EditText mEditText;

    // 定义5个记录当前时间的变量
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏  
		setContentView(R.layout.book_date_time);
		initView();
	}
	  private void initView() {
	        mDatePicker = (DatePicker) findViewById(R.id.dp_book_date);
	        mTimePicker = (TimePicker) findViewById(R.id.tp_book_time);
	        mEditText =(EditText) findViewById(R.id.et_book_time);
	        mTimePicker.setIs24HourView(true);
	        // 获取当前的年、月、日、小时、分钟
	        Calendar c = Calendar.getInstance();
	        year = c.get(Calendar.YEAR);
	        month = c.get(Calendar.MONTH);
	        day = c.get(Calendar.DAY_OF_MONTH);
	        hour = c.get(Calendar.HOUR);
	        minute = c.get(Calendar.MINUTE);
	        Log.i("info", month+"");
	        //初始化DatePicker组件，初始化时指定监听器
	        mDatePicker.init(year, month, day, new OnDateChangedListener() {
	            @Override
	            public void onDateChanged(DatePicker arg0, int year, int month,
	                    int day) {
	            	BookTimeActivity.this.year = year;
	            	BookTimeActivity.this.month = month;
	            	BookTimeActivity.this.day = day;
	                // 显示当前日期、时间
	                showDate(year, month, day, hour, minute);
	            }
	        });
	        
	        // 为TimePicker指定监听器
	        mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
	            @Override
	            public void onTimeChanged(TimePicker arg0, int hour, int minute) {
	            	BookTimeActivity.this.hour = hour;
	            	BookTimeActivity.this.minute = minute;
	                // 显示当前日期、时间
	                showDate(year, month, day, hour, minute);
	            }
	        });
	    }

	    
	    // 定义在EditText中显示当前日期、时间的方法
	    private void showDate(int year, int month, int day, int hour, int minute) {
	    	Date date=new Date(year-1900, month, day, hour, minute);
	    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	String dateTime = df.format(date);
//	        String str=("日期为：" + year + "年" + (month+1 )+ "月" + day + "日  "
//	                + hour + "时" + minute + "分");
	        mEditText.setText(dateTime);
	    }
	    
	    @SuppressLint("SimpleDateFormat")
		public void doClick(View v){
	    	@SuppressWarnings("deprecation")
			Date date=new Date(year-1900, month, day, hour, minute);
	    	Long time=date.getTime();
	    	Intent intent=new Intent();
	    	intent.putExtra("time", time);
	    	setResult(1001, intent);
	    	BookTimeActivity.this.finish();
	    }

}
